<template>
  <div class="container">
    <div class="form-wrapper">
      <div class="input-group">
        <label class="input-label">文件名字:</label>
        <input v-model="paperName" type="text" placeholder="输入文件名字" />
      </div>
      <div class="input-group abstract-group">
        <label class="input-label">文件摘要:</label>
        <textarea v-model="abstract" rows="4" placeholder="输入文件摘要"></textarea>
      </div>

      <div class="input-group">
        <label class="input-label">授权信息:</label>
        <input v-model="users" type="text" placeholder="输入授权信息" />
      </div>
      <div class="input-group file-group">
        <input type="file" @change="onFileChange" multiple />
        <span v-if="fileName">{{ fileName }}</span>
      </div>
      <button class="submit-button" @click="submit">提交</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      paperName: '',
      abstract: '',
      users: '',
      fileName: '',
      files: []
    };
  },
  methods: {
    onFileChange(e) {
      this.files = e.target.files;
      this.fileName = `${this.files.length} 文件已选择`;
    },
    async submit() {
      const formData = new FormData();
      formData.append('paperName', this.paperName);
      formData.append('abstract', this.abstract);
      formData.append('users', this.users);
      Array.from(this.files).forEach((file, index) => {
        formData.append(`files`, file);
      });

      try {
        const response = await fetch('http://192.168.124.128:8081/api/upload', {
          method: 'POST',
          headers: {
            'Authorization': `Bearer ${localStorage.getItem('token')}`,
          },
          body: formData
        });

        if (response.ok) {
          alert("提交成功");
          location.reload();
        } else {
          const data = await response.json();
          alert(`提交失败: ${data.error}`);
        }
      } catch (error) {
        console.error('Error:', error);
        alert("发生错误");
      }
    }
  }
};
</script>
  
<style scoped>
.container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 90vh;
    background-color: #f7f8fa;
}

.form-wrapper {
    background-color: #fff;
    padding: 40px;
    border-radius: 5px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    max-width: 500px;
    width: 100%;
}

.input-group {
    margin-bottom: 15px;
    display: flex;
    align-items: center;
}

.input-label {
    width: 100px;
    text-align: right;
    margin-right: 10px;
}

input[type="text"],
textarea {
    flex: 1;
    padding: 10px;
    border: 1px solid #d1d5db;
    border-radius: 4px;
    font-size: 14px;
    margin-top: 5px;
}

.abstract-group textarea {
    height: 100px;
}

.submit-button {
    background-color: #3498db;
    color: #fff;
    border: none;
    padding: 10px 20px;
    border-radius: 5px;
    cursor: pointer;
    transition: background-color 0.3s ease;
    display: block;
    margin: 0 auto;
}

.submit-button:hover {
    background-color: #2980b9;
}


.file-group {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin: 20px 0;
}
</style>
  